package com.example.fitapp.activity.http.util;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.example.fitapp.activity.activity.MyApplication;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Call;

public abstract class ApiUtil {
    private ApiListener mApiListener = null;
    private String mStatus;
    private Context mContext = null;
    private OkHttpCallback mSendListener = new OkHttpCallback() {

        @Override
        protected boolean isPostMainThread() {
            return isBackInMainThread();
        }

        @Override
        public void onSuccess(Call call, JSONObject response) {
            if(response != null){
                mStatus = response.optString("error_code");
                try {
                    parseData(response);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (isSuccess()) {
                    if(mApiListener!= null){
                        mApiListener.success(ApiUtil.this);
                    }
                } else {
                    if(mApiListener != null){
//                       在这里解析回调，添加提示
                        String msg = response.optString("msg");
                        if(msg!=null){
                            Toast.makeText(MyApplication.getContext(),msg,Toast.LENGTH_SHORT).show();
                        }
                        mApiListener.failure(ApiUtil.this);
                    }
                }
            }
            Log.i("Onsuccess","res null");
        }

        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {
            Toast.makeText(MyApplication.getContext(),"服务器异常,请联系管理员",Toast.LENGTH_SHORT).show();
            mApiListener.failure(ApiUtil.this);
        }
    };

    protected boolean isBackInMainThread() {
        return true;
    }

    public boolean isSuccess() {
        return "0".equals(mStatus) || "200".equals(mStatus);
    }

    protected abstract void parseData(JSONObject jsonObject) throws Exception;

    protected abstract String getUrl();

    private HashMap<String, String> mBodyMap = new HashMap<>();

    public void addParams(String key, String value) {
        mBodyMap.put(key, value);
    }

    //发送get请求
    public void get( ApiListener listener) {
//        mContext = context;
        mApiListener = listener;
        String url = UrlConfig.getBaseUrl()+getUrl();
        OkHttpUtil.get(url, mSendListener);
//        if (Util.hasNetwork(mContext)) {
//            OkHttpUtil.get(getUrl(), mSendListener);
//        }
    }

    public void post(ApiListener listener) {
        mApiListener = listener;
        String url = UrlConfig.getBaseUrl()+getUrl();
        OkHttpUtil.post(url, mSendListener, mBodyMap);
    }


    public void put(ApiListener listener) {
        mApiListener = listener;
        String url = UrlConfig.getBaseUrl()+getUrl();
        OkHttpUtil.put(url, mSendListener, mBodyMap);

    }

    public void delete(ApiListener listener) {
        mApiListener = listener;
        String url = UrlConfig.getBaseUrl()+getUrl();
        OkHttpUtil.delete(url, mSendListener, mBodyMap);

    }
}